import 'dart:io';

import 'package:dio/dio.dart';
import 'package:enterprise_v6_app/app/components/photo_show.dart';
import 'package:enterprise_v6_app/app/components/toast_widget.dart';
import 'package:enterprise_v6_app/app/components/video/ijk_player.dart';
import 'package:enterprise_v6_app/app/core/utils/interface.dart';
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
import 'package:get/get.dart';
import 'package:open_filex/open_filex.dart';
import 'package:path_provider/path_provider.dart';

class FileUtils {
  /// 获取文件类型
  static String? getFileType(String path) {
    if (path.isEmpty) {
      return null;
    }
    final int index = path.lastIndexOf('.');
    if (index == -1) {
      return null;
    }
    return path.substring(index + 1);
  }

  /// 根据文件类型打开文件
  /// [path] 文件路径 可以自动判断是否有http
  /// [title] 标题
  static openFile(String? path,{String? title}) {
    if(path == null || path.isEmpty) {
      AppToast.showWarning('暂无文件!');
      return;
    }
    final String? type = getFileType(path);
    if (type == null) {
      return null;
    }
    if(!path.startsWith('http')) {
      path = '${Interface.fileUrl}$path';
    }
    switch (type) {
      case 'pdf' || 'PDF':
        return downloadFile(path, type: 'application/pdf');
      case 'doc':
        return downloadFile(path, type: 'application/msword');
      case 'docx':
        return downloadFile(path, type: 'application/vnd.openxmlformats-officedocument.wordprocessingml.document');
      case 'xls':
        return downloadFile(path, type: 'application/vnd.ms-excel');
      case 'xlsx':
        return downloadFile(path, type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
      case 'ppt':
        return downloadFile(path, type: 'application/vnd.ms-powerpoint');
      case 'pptx':
        return downloadFile(path, type: 'application/vnd.openxmlformats-officedocument.presentationml.presentation');
      case 'txt':
        return downloadFile(path);
      case 'apk':
        return downloadFile(path, type: 'application/vnd.android.package-archive');
      case 'jpg' || 'jpeg' || 'png' || 'gif' || 'jfif' || 'webp':
        return Get.to(() => PhotoShowPage(url: path!, title: title));
      case 'mp4' || 'avi':
        return Get.to(() => IjkPlayerPage(url: path!,));
      default:
        return downloadFile(path);
    }
  }

  //根据下载路径获取保存的文件路径
  static Future<String> getSavePath(String url) async {
    final filename = url.substring(url.lastIndexOf('/') + 1);
    // 替换为你要保存的文件路径
    final Directory directory = await getApplicationDocumentsDirectory();
    final String savePath = '${directory.path}/$filename';
    return savePath;
  }

  /// 从网络上下载文件
  static Future<String> downloadFile(String url, {String? type,bool isOpen = true,Function(int, int)? onReceiveProgress,bool isShowLoading = true}) async {
    final String savePath = await getSavePath(url);

    if(!isOpen) {
      return savePath;
    }

    ///  扫描文件是否存在  [isExist]为true存在，为false不存在
    final bool isExist = await File(savePath).exists();
    // 不存在就下载 存在就直接打开
    if(isExist) {
      if(isOpen) {
        OpenFilex.open(savePath,type: type);
      }
      return savePath;
    }
    if(isShowLoading){
      SmartDialog.showLoading(msg: '正在下载...');
    }
    await Dio().download(url, savePath, onReceiveProgress: onReceiveProgress);
    if(isShowLoading){
      SmartDialog.dismiss();
    }
    if(isOpen) {
      OpenFilex.open(savePath,type: type);
    }
    return savePath;
  }

  /// 删除文件
  static Future delete(String? url) async {
    if(url == null) {
      return;
    }
    final String? type = getFileType(url);
    if (type == null) {
      return null;
    }
    final String filename = url.substring(url.lastIndexOf('/') + 1);
    // 替换为你要保存的文件路径
    final Directory directory = await getApplicationDocumentsDirectory();
    final String savePath = '${directory.path}/$filename';
    File(savePath).delete();
  }

}
